<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .carousel {
      margin: 100px auto;
      width: 800px;
      height: 350px;
      border: 10px solid orange;
    }

    .cal-list {
      position: relative;
      left: 0;
      top: 0;
      width: 800px;
      height: 350px;
      /* overflow: hidden; */
    }

    .viewpage {
      position: absolute;
      left: 0;
      top: 0;
      width: 700%;
      height: 350px;
    }

    .item {
      float: left;
    }

    .prev,
    .next {
      width: 30px;
      height: 50px;
      background-color: rgba(255, 255, 255, 0.5);
      position: absolute;
      top: 40%;
      font-size: 24px;
      color: #fff;
      text-align: center;
      line-height: 50px;
      border-radius: 0 25px 25px 0;
      display: none;
      cursor: pointer;
    }

    .next {
      right: 0;
      border-radius: 25px 0 0 25px;
    }

    img {
      width: 800px;
      height: 350px;
    }
  </style>

</head>

<body>
  <div class="carousel">
    <div class="cal-list">
      <ul class="viewpage">
        <li class="item"><img src="./img/bc1.png" alt="" /></li>
        <li class="item"><img src="./img/bc2.png" alt="" /></li>
        <li class="item"><img src="./img/bc3.png" alt="" /></li>
        <li class="item"><img src="./img/bc4.png" alt="" /></li>
        <li class="item"><img src="./img/bc5.png" alt="" /></li>
        <li class="item"><img src="./img/bc6.png" alt="" /></li>
        <li class="item"><img src="./img/bc1.png" alt="" /></li>
      </ul>
      <!-- 放置左右按钮 -->
      <div class="prev">&lt;</div>
      <div class="next">&gt;</div>
    </div>
  </div>

  <script src="./js/animationUtil.js"></script>
  <script>
    var viewpage = document.getElementsByClassName("viewpage")[0]
    var calList = document.getElementsByClassName("cal-list")[0]
    var prev = document.getElementsByClassName("prev")[0]
    var next = document.getElementsByClassName("next")[0]
    var items = document.getElementsByClassName("item");

    var index = 0;

    // 1 自动轮播
    var inteId
    function autoPlay () {
      inteId = setInterval(function () {
        index++;
        if (index >= items.length) {
          viewpage.style.left = "0px"
          index = 1;
        }

        var target = -800 * index;
        animationMove(viewpage, target)
      }, 2000)
    }

    autoPlay()


    //2 悬停暂停, 离开继续, 3 鼠标悬停左右按钮显示,离开隐藏
    calList.onmouseover = function (e) {
      clearInterval(inteId)
      prev.style.display = "block"
      next.style.display = "block"
    }
    calList.onmouseleave = function (e) {
      autoPlay()
      prev.style.display = "none"
      next.style.display = "none"
    }

    //3 点击下一个调用autoPlay
    next.onclick = function () {
      index++;
      if (index >= items.length) {
        viewpage.style.left = "0px"
        index = 1;
      }

      var target = -800 * index;
      console.log(target, index, items.length);
      animationMove(viewpage, target)
    }
    //3 点击上一个图片,
    prev.onclick = function () {

      if (index <= 0) {
        index = items.length - 1;
        viewpage.style.left = -800 * index + "px"
      }
      index--;
      var target = -800 * index;
      console.log(target, index, items.length);
      animationMove(viewpage, target)
    }

  </script>

</body>

</html>